package com.caipiao.lottery.config;

/**
 * @author GaoFeng
 */

import java.io.File;
import java.util.Properties;
import com.caipiao.lottery.log.TraceLogger;

import jodd.props.Props;

public class PropConfig extends ConfigInterface
{
	//锁
	/*
	protected ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
	protected Lock readLock = rwl.readLock();
	protected Lock writeLock = rwl.writeLock();
	*/
  
	//配置数据
	protected Props prop =new Props();
	
	//构造
	public PropConfig(String name)
	{
		this.ConfigName = name;
	}
	
	/*
	public Props GetProps()
	{
		synchronized(lock)
		{
			return this.prop;
		}
	}*/
	
    public Props GetProps()
    {
    	return this.prop;
    }
	
	public String GetPropValue(String key)
	{
		try
		{
		//	this.readLock.lock();
			
			Props prop_ = this.prop;
			String value = prop_.getValue(key);
			
			if (null == value)
			{
				return "";
			}
			return value;
		}
		finally
		{
	//		this.readLock.unlock();
		}
	}
	
	public String GetPropValue(String key, String profiles)
	{
		try
		{
			//this.readLock.lock();
			
			Props prop_ = this.prop;
			String value = prop_.getValue(key, profiles);
			
			if (null == value)
			{
				return "";
			}
			return value;
		}
		finally
		{
			//this.readLock.unlock();
		}
	}
	
	public Properties GetPropById(String profiles)
	{
		Properties p = new Properties();
		try
		{
		//	this.readLock.lock();
			Props prop_ = this.prop;
			prop_.extractProps(p, profiles);
		}
		finally
		{
		//	this.readLock.unlock();
		}
		
		return p;
	}
	
	public Properties GetSubPropById(String pattern, String profiles)
	{
		Properties p = new Properties();

		try
		{
			//this.readLock.lock();
			Props prop_ = this.prop;
			prop_.extractSubProps(p, pattern, profiles);
		}
		finally
		{
			//this.readLock.unlock();
		}
		
		return p;
	}
  
	//重加载
	public boolean ReLoad()
	{
        try
        {
        	File cfgFile = new File(this.FilePath);
        	
        	this.lastModify = cfgFile.lastModified();
        	
        	Props prop_ = new Props();
        	
        	prop_.load(cfgFile);
        	
        	try
        	{
        		//writeLock.lock();
        		this.prop = prop_;
        	}
        	finally
        	{
        		//writeLock.unlock();
        	}
        	TraceLogger.System.debug(ConfigName + "加载完成:" + FilePath);
        	
            return true;
        }
        catch (Exception e)
        {
        	TraceLogger.System.error(ConfigName + "加载异常:" + FilePath + "|" + e.toString(), e);
        }
        
        return false;
	}
}
